using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace PackTracker.Business
{ 
	[Serializable()] 
	public class ItemMonitoramento : Csla.BusinessBase<ItemMonitoramento>
	{
		#region Business Properties and Methods

		//declare members
		private int _id = 0;
		private int _idMonitoramento = 0;
		private int _idItemPedido = 0;

		//declare borrowed child member(s)
		private SmartDate _idMonitoramentoData;
		private int _idItemPedidoQuantidade;

		[System.ComponentModel.DataObjectField(true, true)]
		public int Id
		{
			get
			{
				CanReadProperty("Id", true);
				return _id;
			}
		}

		public int IdMonitoramento
		{
			get
			{
				CanReadProperty("IdMonitoramento", true);
				return _idMonitoramento;
			}
			set
			{
				CanWriteProperty("IdMonitoramento", true);
				if (!_idMonitoramento.Equals(value))
				{
					_idMonitoramento = value;
					PropertyHasChanged("IdMonitoramento");
				}
			}
		}

		public int IdItemPedido
		{
			get
			{
				CanReadProperty("IdItemPedido", true);
				return _idItemPedido;
			}
			set
			{
				CanWriteProperty("IdItemPedido", true);
				if (!_idItemPedido.Equals(value))
				{
					_idItemPedido = value;
					PropertyHasChanged("IdItemPedido");
				}
			}
		}

		public DateTime idMonitoramentoData
		{
			get
			{
				CanReadProperty("idMonitoramentoData", true);
				return _idMonitoramentoData.Date;
			}
		}

		public string idMonitoramentoDataString
		{
			get
			{
				CanReadProperty("idMonitoramentoDataString", true);
				return _idMonitoramentoData.Text;
			}
		}

		public int idItemPedidoQuantidade
		{
			get
			{
				CanReadProperty("idItemPedidoQuantidade", true);
				return _idItemPedidoQuantidade;
			}
		}
 
		protected override object GetIdValue()
		{
			return _id;
		}

		#endregion //Business Properties and Methods

		#region Validation Rules
		private void AddCustomRules()
		{
			//add custom/non-generated rules here...
		}

		private void AddCommonRules()
		{
			//
			// IdMonitoramento
			//
			ValidationRules.AddRule(CommonRules.IntegerMinValue, new CommonRules.IntegerMinValueRuleArgs("IdMonitoramento", 1));
			//
			// IdItemPedido
			//
			ValidationRules.AddRule(CommonRules.IntegerMinValue, new CommonRules.IntegerMinValueRuleArgs("IdItemPedido", 1));
		}

		protected override void AddBusinessRules()
		{
			AddCommonRules();
			AddCustomRules();
		}
		#endregion //Validation Rules

		#region Authorization Rules
		protected override void AddAuthorizationRules()
		{
			//TODO: Define authorization rules in ItemMonitoramento
			//AuthorizationRules.AllowRead("Id", "ItemMonitoramentoReadGroup");
			//AuthorizationRules.AllowRead("IdMonitoramento", "ItemMonitoramentoReadGroup");
			//AuthorizationRules.AllowRead("IdItemPedido", "ItemMonitoramentoReadGroup");

			//AuthorizationRules.AllowWrite("IdMonitoramento", "ItemMonitoramentoWriteGroup");
			//AuthorizationRules.AllowWrite("IdItemPedido", "ItemMonitoramentoWriteGroup");
		}


		public static bool CanGetObject()
		{
			//TODO: Define CanGetObject permission in ItemMonitoramento
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoViewGroup"))
			//	return true;
			//return false;
		}

		public static bool CanAddObject()
		{
			//TODO: Define CanAddObject permission in ItemMonitoramento
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoAddGroup"))
			//	return true;
			//return false;
		}

		public static bool CanEditObject()
		{
			//TODO: Define CanEditObject permission in ItemMonitoramento
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoEditGroup"))
			//	return true;
			//return false;
		}

		public static bool CanDeleteObject()
		{
			//TODO: Define CanDeleteObject permission in ItemMonitoramento
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoDeleteGroup"))
			//	return true;
			//return false;
		}
		#endregion //Authorization Rules

		#region Factory Methods
		private ItemMonitoramento()
		{ /* require use of factory method */ }

		public static ItemMonitoramento NewItemMonitoramento()
		{
			if (!CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a ItemMonitoramento");
			return DataPortal.Create<ItemMonitoramento>();
		}

		public static ItemMonitoramento GetItemMonitoramento(int id)
		{
			if (!CanGetObject())
				throw new System.Security.SecurityException("User not authorized to view a ItemMonitoramento");
			return DataPortal.Fetch<ItemMonitoramento>(new Criteria(id));
		}

		public static void DeleteItemMonitoramento(int id)
		{
			if (!CanDeleteObject())
				throw new System.Security.SecurityException("User not authorized to remove a ItemMonitoramento");
			DataPortal.Delete(new Criteria(id));
		}

		public override ItemMonitoramento Save()
		{
			if (IsDeleted && !CanDeleteObject())
				throw new System.Security.SecurityException("User not authorized to remove a ItemMonitoramento");
			else if (IsNew && !CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a ItemMonitoramento");
			else if (!CanEditObject())
				throw new System.Security.SecurityException("User not authorized to update a ItemMonitoramento");

			return base.Save();
		}

		#endregion //Factory Methods

		#region Child Factory Methods
		internal static ItemMonitoramento NewItemMonitoramentoChild()
		{
			ItemMonitoramento child = new ItemMonitoramento();
			child.ValidationRules.CheckRules();
			child.MarkAsChild();
			return child;
		}

		internal static ItemMonitoramento GetItemMonitoramento(SafeDataReader dr)
		{
			ItemMonitoramento child =  new ItemMonitoramento();
			child.MarkAsChild();
			child.Fetch(dr);
			return child;
		}
		#endregion //Child Factory Methods

		#region Data Access

		#region Criteria

		[Serializable()]
		private class Criteria
		{
			public int Id;

			public Criteria(int id)
			{
				this.Id = id;
			}
		}

		#endregion //Criteria

		#region Data Access - Create
		[RunLocal]
		protected override void DataPortal_Create()
		{
			ValidationRules.CheckRules();
		}

		#endregion //Data Access - Create

		#region Data Access - Fetch
		private void DataPortal_Fetch(Criteria criteria)
		{
			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				ExecuteFetch(cn, criteria);
			}//using
		}

		private void ExecuteFetch(SqlConnection cn, Criteria criteria)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_GetItemMonitoramento";
				cm.Parameters.AddWithValue("@id", criteria.Id);

				using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
				{
					dr.Read();
					FetchObject(dr);
					ValidationRules.CheckRules();

					//load child object(s)
					FetchChildren(dr);
				}
			}//using
		}

		#endregion //Data Access - Fetch

		#region Data Access - Insert
		protected override void DataPortal_Insert()
		{
			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				ExecuteInsert(cn);

				//update child object(s)
				UpdateChildren(cn);
			}//using

		}

		#endregion //Data Access - Insert

		#region Data Access - Update
		protected override void DataPortal_Update()
		{
			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				if (base.IsDirty)
				{
					ExecuteUpdate(cn);
				}

				//update child object(s)
				UpdateChildren(cn);
			}//using

		}

		#endregion //Data Access - Update

		#region Data Access - Delete
		protected override void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new Criteria(_id));
		}

		private void DataPortal_Delete(Criteria criteria)
		{
			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				ExecuteDelete(cn, criteria);

			}//using

		}

		private void ExecuteDelete(SqlConnection cn, Criteria criteria)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_DeleteItemMonitoramento";

				cm.Parameters.AddWithValue("@id", criteria.Id);

				cm.ExecuteNonQuery();
			}//using
		}
		#endregion //Data Access - Delete

		#region Data Access - Fetch
		private void Fetch(SafeDataReader dr)
		{
			FetchObject(dr);
			MarkOld();
			ValidationRules.CheckRules();

			//load child object(s)
			if(!this.IsChild)
				FetchChildren(dr);
		}

		private void FetchObject(SafeDataReader dr)
		{
			_id = dr.GetInt32("id");
			_idMonitoramento = dr.GetInt32("idMonitoramento");
			_idItemPedido = dr.GetInt32("idItemPedido");
			_idMonitoramentoData = dr.GetSmartDate("idMonitoramentoData", _idMonitoramentoData.EmptyIsMin);
			_idItemPedidoQuantidade = dr.GetInt32("idItemPedidoQuantidade");
		}

		private void FetchChildren(SafeDataReader dr)
		{
		}
		#endregion //Data Access - Fetch

		#region Data Access - Insert
		internal void Insert(SqlConnection cn)
		{
			if (!IsDirty) return;

			ExecuteInsert(cn);
			MarkOld();

			//update child object(s)
			UpdateChildren(cn);
		}

		private void ExecuteInsert(SqlConnection cn)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_AddItemMonitoramento";

				AddInsertParameters(cm);

				cm.ExecuteNonQuery();

				_id = (int)cm.Parameters["@id"].Value;
			}//using
		}

		private void AddInsertParameters(SqlCommand cm)
		{
			cm.Parameters.AddWithValue("@idMonitoramento", _idMonitoramento);
			cm.Parameters.AddWithValue("@idItemPedido", _idItemPedido);
			cm.Parameters.AddWithValue("@id", _id);
			cm.Parameters["@id"].Direction = ParameterDirection.Output;
		}
		#endregion //Data Access - Insert

		#region Data Access - Update
		internal void Update(SqlConnection cn)
		{
			if (!IsDirty) return;

			if (base.IsDirty)
			{
				ExecuteUpdate(cn);
				MarkOld();
			}

			//update child object(s)
			UpdateChildren(cn);
		}

		private void ExecuteUpdate(SqlConnection cn)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_UpdateItemMonitoramento";

				AddUpdateParameters(cm);

				cm.ExecuteNonQuery();

			}//using
		}

		private void AddUpdateParameters(SqlCommand cm)
		{
			cm.Parameters.AddWithValue("@id", _id);
			cm.Parameters.AddWithValue("@idMonitoramento", _idMonitoramento);
			cm.Parameters.AddWithValue("@idItemPedido", _idItemPedido);
		}

		private void UpdateChildren(SqlConnection cn)
		{
		}
		#endregion //Data Access - Update

		#region Data Access - Delete
		internal void DeleteSelf(SqlConnection cn)
		{
			if (!IsDirty) return;
			if (IsNew) return;

			ExecuteDelete(cn, new Criteria(_id));
			MarkNew();
		}
		#endregion //Data Access - Delete
		#endregion //Data Access
	}
}
